﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.Reporting.WinForms;

namespace QldtSdh.Utils
{
    class PrintUtil
    {
        public static string WORDFORMAT = "WORD";
        public static string PDFFORMAT = "PDF";
        public static string EXCELFORMAT = "EXCEL";
        /// <summary>
        /// Lấy tên máy in mặc định
        /// </summary>
        /// <returns></returns>
        public static string GetDefaultPrinter()
        {
            PrinterSettings settings = new PrinterSettings();
            foreach (string printer in PrinterSettings.InstalledPrinters)
            {
                settings.PrinterName = printer;
                if (settings.IsDefaultPrinter)
                    return printer;
            }
            return string.Empty;
        }

        /// <summary>
        /// Xuất dữ liệu trong report ra file
        /// </summary>
        /// <param name="report">Dữ liệu sẽ xuất ra</param>
        /// <param name="typeDoc">Kiểu tài liệu muốn xuaart {PDF, WORD, IMAGE, EXCEL}</param>
        /// <param name="fileName">Đường dẫn tạm thời để lưu file</param>
        public static void Export(LocalReport report, string typeDoc, string fileName)
        {
            string deviceInfo =
              "<DeviceInfo>" +
              "  <OutputFormat>EMF</OutputFormat>" +
              "  <PageWidth>8.27in</PageWidth>" +
              "  <PageHeight>11.7in</PageHeight>" +
              "  <MarginTop>0.3in</MarginTop>" +
              "  <MarginLeft>0.2in</MarginLeft>" +
              "  <MarginRight>0.2in</MarginRight>" +
              "  <MarginBottom>0.2in</MarginBottom>" +
              "</DeviceInfo>";
            var bytes = report.Render(typeDoc, deviceInfo);
            using (var fs = new FileStream(fileName, FileMode.Create))
            {
                fs.Write(bytes, 0, bytes.Length);
                fs.Close();
            }
        }
    }
}
